<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item v-if="!isShowOperate" label="学校" prop="schoolId">
        <el-select v-model="queryParams.schoolId" placeholder="请选择学校" clearable size="small">
          <el-option v-for="dict in schoolOptions" :key="dict.id" :label="dict.name" :value="dict.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="安全隐患级别" prop="safetyHazardLevel">
        <el-select v-model="queryParams.safetyHazardLevel" placeholder="请选择安全隐患级别" clearable size="small">
          <el-option v-for="dict in safetyHazardLevelOptions" :key="dict.dictValue" :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"-->
      <!--                   v-hasPermi="['patrol:reporting:add']" v-if="isShowOperate">新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['patrol:reporting:export']"
      >导出
      </el-button>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['patrol:reporting:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['patrol:reporting:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="reportingList" style="color: #333333"
              :header-cell-style="{background:'#E3E3E3'}">
      <el-table-column show-overflow-tooltip label="标题" align="center">
        <template slot-scope="scope">
          {{ formatTitle(scope.row.createTime, scope.row.schoolName, scope.row.safetyHazardLevel) }}
        </template>
      </el-table-column>
      <el-table-column label="学校" v-if="!this.$auth.hasPermi('patrol:reporting:edit')" align="center"
                       prop="schoolName"/>
      <!--      <el-table-column label="上报人" align="center" prop="submitter"/>-->
      <el-table-column label="安全隐患级别" align="center" prop="safetyHazardLevel">
        <template slot-scope="scope">
          <template v-for="dict in safetyHazardLevelOptions">
            <template v-if="scope.row.safetyHazardLevel == dict.dictValue"><span>{{ dict.dictLabel }}</span></template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="发现时间" align="center" prop="findtime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.findtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改状态" align="center" prop="statetype">
        <template slot-scope="scope">
          {{ scope.row.statetype == 1 ? "整改完成" : scope.row.statetype == 0 ? "整改中" : "未整改" }}
          <span v-if="getTimeType(scope.row)" style="color: red">整改超时</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDetail(scope.row)">查看
          </el-button>
          <el-button size="mini" v-if="scope.row.statetype!=1" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['patrol:reporting:edit']">修改
          </el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['patrol:reporting:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import {
  listReporting,
  delReporting,
  exportForm
} from "@/api/patrol/reporting";

import {
  selectDictLabel
} from "@/utils/ruoyi";

import {
  listSchool
} from "@/api/patrol/school";

import {
  checkPermi
} from "@/utils/permission";


export default {
  name: "Reporting",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全隐患上报表格数据
      reportingList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolId: null,
        safetyHazardLevel: null
      },
      // 安全隐患级别字典
      safetyHazardLevelOptions: [],
      // 学校字典
      schoolOptions: [],
      // 查询学校参数
      querySchoolParams: {
        pageNum: 1,
        name: null,
        type: null,
        nature: null,
        userId: null,
      },
    };
  },
  computed: {
    isShowOperate: function () {
      return checkPermi(['patrol:reporting:add']);
    },
  },
  created() {
    if (!this.isShowOperate) {
      this.getSchoolOptions();
    }
    this.getSafetyHazardLevelOptions();
    this.getList();
  },
  watch: {
    "$route": {
      handler(new_value, old_value) {
        if (old_value && old_value.name == "AddReporting") {
          this.getList();
        }
      }
    }
  },
  methods: {
    getTimeType(data) {
      if (data.updateTime == null) {
        if (new Date(data.completetime).getTime() < new Date().getTime()) {
          return true;
        }
      } else if (new Date(data.completetime).getTime() < new Date(data.updateTime).getTime()) {
        return true;
      }
    },
    /** 查询安全隐患上报列表 */
    getList() {
      this.loading = true;
      listReporting(this.queryParams).then(response => {
        this.reportingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        path: "/reporting/add",
        query: {
          reportingId: "false"
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$tab.openPage("编辑安全隐患上报", "/reporting/add", {
        reportingId: row.id
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除安全隐患上报编号为"' + ids + '"的数据项？').then(function () {
        return delReporting(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有单数据项?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      }).then(function () {
        return exportForm(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    /** 获取安全隐患级别列表 */
    getSafetyHazardLevelOptions() {
      this.loading = true;
      this.getDicts("safety_hazard_level").then(response => {
        this.safetyHazardLevelOptions = response.data;
        this.loading = false;
      });
    },
    /** 获取学校列表 */
    getSchoolOptions() {
      this.loading = true;
      listSchool(this.querySchoolParams).then(response => {
        this.schoolOptions = response.rows;
        this.loading = false;
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.$router.push({
        name: 'ReportingDetail',
        path: "/reporting/detail",
        query: {
          id: encodeURI(JSON.stringify(row.id))
        }
      });
    },
    formatTitle(date, school, level) {
      date = date.split(" ")[0].split("-");
      if (date[1] - '0' < 10) {
        date[1] = date[1] - '0';
      }
      if (date[2] - '0' < 10) {
        date[2] = date[2] - '0';
      }
      date = date[0] + "年" + date[1] + "月" + date[2] + "日";
      level = selectDictLabel(this.safetyHazardLevelOptions, level)
      return date + school + level + "安全隐患报告";
    },
  }
};

</script>
